<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>

<%@ taglib uri="/struts-tags" prefix="s"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>

<head>

<link href="${pageContext.request.contextPath}/css/sub_detalie_list.css"
	rel="stylesheet" type="text/css" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>试题详情页面</title>
</head>
<body onload="loadingSubDetaile()">

	<div class="main">
		<%@ include file="header.jsp"%>

		<s:if test="#session.userModel == null">
			<div class="div_user_no_login_main">
				<div onclick="toLoginPage()">请登录</div>
			</div>
		</s:if>
		<s:else>
			<div class="div_show_sub_detaile_header">
				<div class="sub_header">
					<span><strong>标题</strong></span><span class="sub_header_in"
						id="sub_header_title">java基础综合测试一</span>
				</div>
				<textarea class="sub_header_alert" id="sub_header_title_alert"
					rows="10" cols="10"></textarea>
				<div class="sub_header">
					<span><strong>时间</strong></span><span class="sub_header_in"
						id="sub_header_time">2017年5月6日</span>

				</div>
				<div class="sub_header">
					<span><strong>总分</strong></span><span class="sub_header_in"
						id="sub_header_total_count">100分</span>
				</div>
				<div class="sub_header">
					<span><strong>题记</strong></span><span class="sub_header_in"
						id="sub_header_title_des">生命原是有所追寻，追寻中没有人能担保，一定是顺利安乐的吃点苦，也许是激励</span>

				</div>
				<textarea class="sub_header_title_des"
					id="sub_header_title_des_alert" rows="10" cols="10"></textarea>

				<div class="sub_header">

					<div class="sub_header_button" onclick="addSubItem()">添加小试题</div>
					<div class="sub_header_button_alert" iscommit="false"
						onclick="alertSubItem(this)">修改</div>
					<div class="sub_header_button_canle" onclick="canleAlert(this)">取消</div>
					<div class="sub_header_button_delete"
						onclick="showDeleteAlert(this)">删除</div>

				</div>

			</div>


			<div class="div_header_and_selet_flag"></div>

			<div class="sub_content_div">
				<table id="table_show_sub_detaile" cellpadding="0" cellspacing="0">
					<tr class="table_header">
						<th class="table_header_frist">序号</th>
						<th class="table_header">试题详情</th>
						<th class="table_header">编辑</th>
					</tr>
				</table>
			</div>

		</s:else>
	</div>






	<script type="text/javascript">
		var subModel;
		/*删除 */
		function showDeleteAlert(time) {
			if (confirm('确定要执行此操作吗?')) {
				var loadView = showLoading("正在计算，请稍候...", function() {

					deleteSubFunction(loadView);

				});

				return true;
			}
			return false;
		}
		function deleteSubFunction(loadViwe) {
			var url = getDeleteSubUrl();
			var uuid = subModel.uSubUid;

			$.post(url, {
				subUUid : encodeURI(uuid)
			}, function(data, textStatus) {
				loadViwe.fadeOut("fast", function() {
					loadViwe.remove();
				});
				if (textStatus == "success") {
					console.log("The textStatus is: " + textStatus);
					console.log("The data is: " + data);
					var code = data.code;
					var message = data.message;
					if (code == "1000") {
						console.log("删除 小试题 成功 ");
						toSubManagerPage();
					} else {
						console.log("The message is: " + message);
						alert("删除试题失败 ..+message");
					}

				} else {
					console.log("The textStatus is: " + textStatus);
					alert("删除试题失败 " + data);
				}
			}, "json");

		}

		/*  取消修改*/
		function canleAlert(time) {
			var $canleBut = $('.sub_header_button_canle');
			$canleBut.css({
				"display" : "none"
			});

			var $but = $('.sub_header_button_alert');
			$but.html("修改");
			$but.attr("iscommit", "false");

			var $subTitle = $('#sub_header_title_alert');
			var $subFlaDes = $('#sub_header_title_des_alert');

			$subTitle.css({
				"display" : "none"
			});
			$subFlaDes.css({
				"display" : "none"
			});
		}
		function alertSubItem(item) {

			if (subModel != null) {
				var $but = $('.sub_header_button_alert');

				var $is = $but.attr("iscommit");

				console.log("修改小试题 : " + $is);

				var $subTitle = $('#sub_header_title_alert');
				var $subFlaDes = $('#sub_header_title_des_alert');
				var $canleBut = $('.sub_header_button_canle');

				if ($is == "false") {
					$but.html("提交");
					$but.attr("iscommit", "true");
					$subTitle.css({
						"display" : "block"
					});
					$subTitle.html(subModel.uSubName);

					$subFlaDes.css({
						"display" : "block"
					});
					$subFlaDes.html(subModel.uSubPreface);

					$canleBut.css({
						"display" : "block"
					});
				} else {
					$canleBut.css({
						"display" : "none"
					});
					alert("commit");

					var $tit = $subTitle.val();
					var $fdes = $subFlaDes.val();

					$tit = $.trim($tit);
					$fdes = $.trim($fdes);
					var uuid = subModel.uSubUid;
					var url = "${pageContext.request.contextPath}/sub_alertMainSub.action?time="
							+ new Date().getTime();
					$
							.post(
									url,
									{
										subTitle : encodeURI($tit),
										subFlagDes : encodeURI($fdes),
										subUUID : encodeURI(uuid)
									},
									function(data, textStatus) {
										if (textStatus == "success") {
											console.log("The textStatus is: "
													+ textStatus);
											console.log("The data is: " + data);
											var code = data.code;
											var message = data.message;
											if (code == "1000") {
												console.log("修改小试题 成功 ");
												var url = "${pageContext.request.contextPath}/index_subDetaileManager.action?time="
														+ new Date().getTime();

												window.location = url;
											} else {
												console.log("The message is: "
														+ message);
											}

										} else {
											console.log("The textStatus is: "
													+ textStatus);
											alert("失败 ");
										}
									}, "json");
				}

			} else {
				alert("试题信息为空, 不可以操作  ..")

			}

		}

		function addSubItem() {
			var uuid = getCookie("uuid");
			console.log("添加小试题 : " + uuid);
			var url = "${pageContext.request.contextPath}/index_addSubItemTo.action?time="
					+ new Date().getTime();
			window.location = url;
		}
		function loadingSubDetaile() {

			var uuid = getCookie("uuid");
			console.log("The uuid is: " + uuid);
			var url = "${pageContext.request.contextPath}/sub_getSubDetaile.action?time="
					+ new Date().getTime();
			$.post(url, {
				subUUid : encodeURI(uuid)
			}, function(data, textStatus) {
				if (textStatus == "success") {
					console.log("The textStatus is: " + textStatus);
					console.log("The data is: " + data);
					var code = data.code;
					var message = data.message;
					if (code == "1000") {
						var obj = data.content;
						subModel = obj;
						showSubDetaileFunction(obj);
					} else {
						console.log("The message is: " + message);
					}

				} else {
					console.log("The textStatus is: " + textStatus);
					alert("失败 ");
				}
			}, "json");
		}

		function showSubDetaileFunction(obj) {

			/* delCookie("sub");
			var sub = JSON.stringify(obj);
			console.log("The sub to json  is " + sub);
			docum ent.cookie = "sub=" + sub;*/

			var uSubIds = obj.uSubIds;
			var uSubImagUrl = obj.uSubImagUrl;
			var uSubItemList = obj.uSubItemList;
			var uSubStartTime = obj.uSubStartTime;
			var uSubUid = obj.uSubUid;
			var uSubName = obj.uSubName;
			var uSubPreface = obj.uSubPreface;
			var formatTime = obj.formatTime;

			var totalNumber = 0;
			for ( var i = 0; i < uSubItemList.length; i++) {
				var item = uSubItemList[i];

				totalNumber += item.itemSubCount;
			}

			$('#sub_header_title').html(uSubName);
			$('#sub_header_time').html(formatTime);

			$('#sub_header_total_count').html("总分 "+totalNumber);

			$('#sub_header_title_des').html(uSubPreface);

			var $table = $('#table_show_sub_detaile');
			for ( var i = 0; i < uSubItemList.length; i++) {
				var itemObj = uSubItemList[i];

				var itemSelectList = itemObj.itemSubSelectList;

				var $tr = $("<tr>" + "<th class='table_sub_item_first'> "
						+ (i + 1)
						+ "</th>"
						+ "<td class='table_sub_item' >"
						+

						"<div>"
						+ "<div>"
						+ "<span class='table_sub_item_title'>"
						+ itemObj.itemSubTitle
						+ "</span>"
						+ "</div>"
						+ "<div class='table_sub_item_select'>"
						+ "<span class='table_sub_item_title_select'>"
						+ itemSelectList[0]
						+ "</span>"
						+ "</div>"

						+ "<div class='table_sub_item_select'>"
						+ "<span class='table_sub_item_title_select'>"
						+ itemSelectList[1]
						+ "</span>"
						+ "</div>"

						+ "<div class='table_sub_item_select'>"
						+ "<span class='table_sub_item_title_select'>"
						+ itemSelectList[2]
						+ "</span>"
						+ "</div>"

						+ "<div class='table_sub_item_select'>"
						+ "<span class='table_sub_item_title_select'>"
						+ itemSelectList[3]
						+ "</span>"
						+ "</div>"

						+ "<div class='table_sub_item_exple'>"
						+ "<strong>解析 </strong> <span class='table_sub_item_exple_span'>正确答案 "
						+ itemObj.itemSubNormalSelect
						+ " </span>"
						+ "</div>"
						+ "</div>"

						+ "</td>"

						+ "<td class='table_sub_item' style='width:10%;text-align:center'>"

						+ "<div class="+uSubUid+">"
						+ "<div id="
						+ uSubUid
						+ i
						+ i
						+ i
						+ " subuuid="
						+ uSubUid
						+ "  uuid ="
						+ itemObj.itemUuid
						+ " class='table_sub_item_select_alert' style='width:80%;text-align:center;color:red;display:none;' >已删除</div>"
						+ "<div id="
						+ uSubUid
						+ i
						+ " subuuid="
						+ uSubUid
						+ " uuid ="
						+ itemObj.itemUuid
						+ " class='table_sub_item_select_alert' style='width:80%;text-align:center' onclick='deleteSub(this)'> 删除</div>"
						+ "<div id="
						+ uSubUid
						+ i
						+ i
						+ " subuuid="
						+ uSubUid
						+ "  uuid ="
						+ itemObj.itemUuid
						+ " class='table_sub_item_select_alert' style='width:80%;text-align:center' onclick='alertSub(this)'>修改</div>"

						+ "</div>"

						+ "</td>"

						+ "</tr>");

				$table.append($tr);
			}

		}

		var deleteSubId;
		function deleteSub(item) {
			var uuid = $(item).attr('uuid');
			var subUuid = $(item).attr('subuuid');
			deleteSubId = $(item).attr('id');

			var url = "${pageContext.request.contextPath}/sub_deleteItem.action?time="
					+ new Date().getTime();
			$
					.post(
							url,
							{
								subUUid : encodeURI(subUuid),
								subItemUUid : encodeURI(uuid)
							},
							function(data, textStatus) {
								if (textStatus == "success") {
									var id = "." + deleteSubId + " ~ div";
									$(id).css("border", "3px groove gray");
									alert("删除成功");

									var url = "${pageContext.request.contextPath}/index_subDetaileManager.action?time="
											+ new Date().getTime();

									window.location = url;
								} else {
									alert("删除失败");
								}
							});
		}
		function alertSub(item) {
			alert("alert");

			var uuid = $(item).attr('uuid');
			var subUuid = $(item).attr('subuuid');

			var url = "${pageContext.request.contextPath}/index_subItemAlertToPae.action?time="
					+ new Date().getTime();
			console.log("The url is: " + url);
			delCookie("alertSubUuid");
			console.log("The delCookie is: alertSubUuid");
			delCookie("alertItemUuid");
			console.log("The delCookie is: alertItemUuid ");
			document.cookie = "alertSubUuid=" + subUuid;
			document.cookie = "alertItemUuid=" + uuid;
			console.log("The save cookie is success ");
			window.location = url;

		}

		function delCookie(name) {
			var exp = new Date();
			exp.setTime(exp.getTime() - 1);
			var cval = getCookie(name);
			if (cval != null)
				document.cookie = name + "=" + cval + ";expires="
						+ exp.toGMTString();
		}

		function getCookie(name) {
			var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
			if (arr = document.cookie.match(reg))
				return unescape(arr[2]);
			else
				return null;
		}
	</script>


</body>
</html>